using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;

namespace BardsTaleMapViewer {
  public enum AlignementH {
    Left,
    Center,
    Right
  }

  public enum AlignementV {
    Top,
    Center,
    Bottom
  }

  public class BitmapFont {
    public int _glyphWidth;
    public int _glyphHeight;
    private Bitmap [] _glyphs;

    public ColorMatrix _colorMatrixColorize;
    public ImageAttributes _imageAttributesColorize;

    public BitmapFont (Bitmap glyphs, int nbGlyphPerLine, int glyphWidth, int glyphHeight, int inter) {
      _glyphWidth = glyphWidth;
      _glyphHeight = glyphHeight;
      _glyphs = new Bitmap [256];
      int i = 0;
      for (int y = 0; i != 256; y++) {
        for (int x = 0; x < nbGlyphPerLine; x++) {
          Bitmap glyph = glyphs.ExtractBitmap (inter + (inter + _glyphWidth) * x, inter + (inter + _glyphHeight) * y, glyphWidth, glyphHeight);
          _glyphs [i] = glyph;
          i++;
        }
      }

      _colorMatrixColorize = new ColorMatrix ();
      _imageAttributesColorize = new ImageAttributes ();
    }

    //public void Draw (Graphics g, int x, int y, string text) {
    //  for (int i = 0; i < text.Length; i++) {
    //    Bitmap b = _glyphs [text [i]];
    //    g.DrawImage (b, x + i * _width, y);
    //    x += _width;
    //  }
    //}

    public void DrawText (Graphics g, int x, int y, string text, Color color, AlignementH alignementH, AlignementV alignementV, Color outlined) {
      if (outlined != Color.Transparent) {
        DrawText (g, x - 1, y, text, outlined, alignementH, alignementV);
        DrawText (g, x + 1, y, text, outlined, alignementH, alignementV);
        DrawText (g, x, y - 1, text, outlined, alignementH, alignementV);
        DrawText (g, x, y + 1, text, outlined, alignementH, alignementV);
      }
      DrawText (g, x, y, text, color, alignementH, alignementV);
    }

    public void DrawText (Graphics g, int x, int y, string text, Color color, AlignementH alignementH, AlignementV alignementV, bool outlined) {
      if (outlined) {
        DrawText (g, x - 1, y, text, Color.Black, alignementH, alignementV);
        DrawText (g, x + 1, y, text, Color.Black, alignementH, alignementV);
        DrawText (g, x, y - 1, text, Color.Black, alignementH, alignementV);
        DrawText (g, x, y + 1, text, Color.Black, alignementH, alignementV);
      }
      DrawText (g, x, y, text, color, alignementH, alignementV);
    }

    private void DrawText (Graphics g, int x, int y, string text, Color color, AlignementH alignementH, AlignementV alignementV) {
      _colorMatrixColorize.Matrix00 = color.R / 255f;
      _colorMatrixColorize.Matrix11 = color.G / 255f;
      _colorMatrixColorize.Matrix22 = color.B / 255f;
      _colorMatrixColorize.Matrix33 = color.A / 255f;
      _imageAttributesColorize.SetColorMatrix (_colorMatrixColorize);

      if (alignementH == AlignementH.Center)
        x -= (text.Length * _glyphWidth) / 2;
      else if (alignementH == AlignementH.Right)
        x -= (text.Length * _glyphWidth);
      if (alignementV == AlignementV.Center)
        y -= (_glyphHeight) / 2;
      else if (alignementV == AlignementV.Bottom)
        y -= (_glyphHeight);

      for (int i = 0; i < text.Length; i++) {
        Bitmap b = _glyphs [text [i]];
        g.DrawImage (b, new Rectangle (x, y, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, _imageAttributesColorize);
        x += _glyphWidth;
      }
    }
  }
}
